<template>
  <div>
    <!-- 搜索 -->
    <HeadSeach ref="headSeach" />
    <div class="app-content">
      <div class="operation">
        <el-button type="primary" class="ml-10" icon="el-icon-plus" size="mini" @click="add">新增</el-button>
        <el-button type="danger" icon="el-icon-delete" size="mini" @click="delBatch">删除
        </el-button>
        <Content>
          <el-table :data="list.data" style="width: 100%" size="small" row-key="id" height="100%" @selection-change="selectionChange">
            <el-table-column type="selection" min-width="30" />
            <el-table-column label="用户名" min-width="50" prop="username" />
            <el-table-column label="真实姓名" min-width="40" prop="real_name" />
            <el-table-column label="所属角色" min-width="60">
              <template slot-scope="scope">
                <p v-if="scope.row.role_info">
                  <label v-for="(item,i) in scope.row.role_info" :key="item.id">{{ item.role_name }}
                    <label v-if="i < scope.row.role_info.length -1">，</label>
                  </label>
                </p>
              </template>
            </el-table-column>
            <el-table-column label="所属部门" min-width="30">
              <template slot-scope="scope">
                <label>{{ scope.row.department_info ?scope.row.department_info.d_name:'' }}</label>
              </template>
            </el-table-column>
            <el-table-column label="状态" min-width="40">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.status == 1" type="success" effect="dark" size="small">
                  启用
                </el-tag>
                <el-tag v-if="scope.row.status == 0" type="warning" effect="dark" size="small">
                  禁用
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="超级管理员" min-width="50">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.is_admin == 1" type="success" effect="dark" size="small">
                  是
                </el-tag>
                <el-tag v-if="scope.row.is_admin == 0" type="warning" effect="dark" size="small">
                  否
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="创建时间" min-width="50" prop="create_time" />
            <el-table-column label="操作" min-width="50">
              <template slot-scope="scope">
                <el-link type="primary" :underline="false" @click="editValue(scope.row.id)">编辑</el-link>
                <el-link type="danger" :underline="false" @click="delValue(scope.row.id)">删除</el-link>
              </template>
            </el-table-column>
          </el-table>
          <pagination ref="pagination" />
        </Content>
      </div>
    </div>
    <!-- 新增/编辑 -->
    <el-dialog :title="param.id?'编辑菜单':'新增菜单'" :visible.sync="dialogAdminVisible" width="520px">
      <el-form ref="form" :model="param" :rules="rules" label-width="150px" class="demo-ruleForm" :show-message="false">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="param.username" size="small" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="real_name">
          <el-input v-model="param.real_name" size="small" />
        </el-form-item>
        <el-form-item label="所属角色" prop="role_id">
          <el-select v-model="param.role_id" placeholder="请选择" clearable filterable collapse-tags multiple>
            <el-option
              v-for="item in listRole"
              :key="item.id"
              :label="item.role_name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属部门">
          <Treeselect
            :props="defaultProps"
            :options="listDepartment"
            :value="param.department_id"
            @getValue="getValue"
          />
        </el-form-item>
        <el-form-item label="登录密码">
          <el-input v-model="param.password" size="small" />
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="param.phone" size="small" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="param.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="超级管理员">
          <el-radio-group v-model="param.is_admin">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogAdminVisible = false">取 消</el-button>
        <el-button type="primary" @click="determine">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>

import Treeselect from '@/components/Treeselect'
import Pagination from '@/components/Pagination'
import { Role } from '@/api/system/role'
import { Admin } from '@/api/system/admin'
import { deleteValue } from '@/utils/public'
import HeadSeach from '@/components/HeadSeach'
export default {
  name: 'Admin',
  components: {
    Treeselect,
    Pagination,
    HeadSeach
  },
  data() {
    return {
      list: { data: [] },
      dialogAdminVisible: false,
      rules: {
        username: { required: true, trigger: 'blur' },
        real_name: { required: true, trigger: 'blur' },
        role_id: { required: true, trigger: 'blur' }
      },
      listRole: [],
      param: {},
      listDepartment: [], // 部门列表
      defaultProps: {
        children: 'children',
        label: 'd_name',
        value: 'id'
      },
      seach: {},
      pagination: {
        page: 1,
        per_page: 30
      }

    }
  },
  created() {
    this.getAdminPage()
  },
  mounted() {
    this.assembleSeachData()
  },
  methods: {
    // 组装搜索数据
    async assembleSeachData() {
      this.listRole = await Role.listRole().then((res) => { return res.data }).catch(() => {})
      const searchData = {
        label: '管理员搜索',
        size: 'small',
        model: {
          username: '',
          real_name: '',
          status: '',
          role_id: ''
        },
        options: [{
          tag: 'Input',
          prop: 'username',
          placeholder: '用户名'
        },
        {
          tag: 'Input',
          prop: 'real_name',
          placeholder: '真实姓名'
        },
        {
          tag: 'Select',
          prop: 'status',
          placeholder: '状态',
          data: [{ label: '启用', value: 1 }, { label: '禁用', value: 0 }],
          showPropData: {
            label: 'label',
            value: 'value'
          }
        },
        {
          tag: 'Select',
          prop: 'role_id',
          placeholder: '角色',
          data: this.listRole,
          showPropData: {
            label: 'role_name',
            value: 'id'
          }
        }],
        cb: (data) => {
          this.seach = data
          this.pagination.page = 1
          this.getAdminPage()
        }
      }
      this.$refs.headSeach.init(searchData)
    },
    getAdminPage() {
      Admin.pageAdmin({ ...this.pagination, ...this.seach }).then((res) => {
        this.list = res.data
        this.initPagination({
          page: res.data.current_page,
          total: res.data.total,
          per_page: res.data.per_page
        })
      }).catch(() => {})
    },

    add() {
      this.param = {
        status: 1,
        is_admin: 1
      }
      this.dialogAdminVisible = true
    },
    // 编辑
    editValue(id) {
      Admin.getAdmin({ id }).then((res) => {
        this.param = res.data
        this.dialogAdminVisible = true
      }).catch(() => {})
    },
    // 删除
    delValue(id) {
      deleteValue({
        _this: this,
        success: (res) => {
          Admin.delAdmin({ id }).then((res) => {
            this.$message.success('删除成功')
            this.getAdminPage()
          }).catch(() => {})
        }
      })
    },
    getValue(val) {
      this.param.department_id = val
    },
    // 选择
    selectionChange(selection) {
      this.selectId = selection.map(val => val.id)
    },
    // 分页
    initPagination(data) {
      this.$refs.pagination.init({ ...data, cb: (res) => {
        this.pagination = res
        this.getAdminPage()
      } })
    },
    delBatch() {
      if (!this.selectId.length) {
        this.$message.error('请先选择要删除的数据,在进行操作！')
        return false
      }
      this.delValue(this.selectId.join(','))
    },
    determine() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          Admin.addAdmin(this.param).then((res) => {
            this.dialogAdminVisible = false
            this.getAdminPage()
          }).catch(() => {})
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
p{
  margin: 0;
}
</style>
